<?php
use app\models\KeyMap;
use app\models\Task;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $model_list app\models\Task[]
 * @var $pagination yii\data\Pagination
 */

$this->title = '定时任务';
$this->params['breadcrumbs'][] = '系统管理';
$this->params['breadcrumbs'][] = $this->title;

app\assets\TableAsset::register($this);
app\assets\AjaxAsset::register($this);
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
    <div class="form-group">
        <button type="submit" class="btn btn-primary btn-sm">搜索</button>
    </div>
<?php echo Html::endForm();?>
<table id="simple-table" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="center">
                <label class="pos-rel">
                    <input type="checkbox" class="ace" />
                    <span class="lbl"></span>
                </label>
            </th>
            <th>用户</th>
            <th>名称</th>
            <th>下次执行时间</th>
            <th>定时</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($model_list as $model) {?>
            <tr id="data_<?php echo $model->id;?>">
                <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $model->id;?>" /><span class="lbl"><?php echo $model->id;?></span></label></td>
                <td><?php echo '(', $model->uid, ')';?></td>
                <td><?php echo Html::encode($model->name);?></td>
                <td><?php echo Yii::$app->formatter->asDatetime($model->next);?></td>
                <td><?php echo Html::encode($model->cron);?></td>
                <td><?php echo Html::a(KeyMap::getValue('task_status', $model->status), 'javascript:void(0)', ['class'=>[Task::STATUS_WAITING=>'label label-success', Task::STATUS_DOING=>'label label-primary', Task::STATUS_FINISHED=>'label label-info', Task::STATUS_PAUSED=>'label label-default'][$model->status]]);?></td>
                <td><?php echo app\widgets\AdminTableOp::widget(['items'=>[
                    ['rbac'=>'system/task', 'icon'=>'fa fa-info', 'href'=>Url::to(['/admin/system/task-view', 'id'=>$model->id]), 'btn_class'=>'btn btn-xs btn-info', 'tip'=>'详情', 'color'=>'blue'],
                    ['rbac'=>'system/task', 'icon'=>'fa fa-trash', 'onclick'=>'deleteTask(' . $model->id . ')', 'btn_class'=>'btn btn-xs btn-danger', 'tip'=>'删除', 'color'=>'red'],
                ]]);?></td>
            </tr>
        <?php }?>
    </tbody>
</table>
<?php echo yii\widgets\LinkPager::widget(['pagination'=>$pagination]);?>
<script>
/**
 * 删除定时任务
 */
function deleteTask(id) {
    if (!confirm('确定要删除吗？')) {
        return false;
    }
    $.getJSON('<?php echo Url::to(['/admin/system/delete-task']);?>', {'id':id}, function(json) {
        if (callback(json)) {
            $('#data_' + id).remove();
        }
    });
}
</script>
